package com.demo.hc.controller.map;

import com.demo.hc.common.util.HttpUtil;
import com.demo.hc.common.web.JsonResult;
import com.demo.hc.controller.base.BaseController;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Description("百度地图API控制器")
@Controller
@RequestMapping("map")
public class MapController extends BaseController {

    private static final String COMMON_BAIDU_URL="http://map.baidu.com/?newmap=1&reqflag=pcmap&from=webmap&qt=ext&uid=";

    @Description("百度地图跳转页面")
    @RequestMapping(value = "index",method= RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "map/index";
    }

    @Description("调用百度地图API")
    @RequestMapping(value = "getMapMessage",method= RequestMethod.GET)
    @ResponseBody
    public JsonResult getMapMessage(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "uid") String uid){
        String url = COMMON_BAIDU_URL + uid +"&ext_ver=new&l=18";
        String geo ="";
        String centerPoint="";
        try {
            Map<String, Object> responseMap = HttpUtil.get(url);
            Map<String, Object> contentMap = MapUtils.getMap(responseMap,"content");
            String geoStr = MapUtils.getString(contentMap,"geo");
            if(StringUtils.isNotEmpty(geoStr)){
                centerPoint = geoStr.substring(geoStr.indexOf("4|") + 2, geoStr.indexOf("|1-"));
                geoStr = geoStr.substring(geoStr.indexOf("1-") + 2, geoStr.length() - 1);
                String arr[]= geoStr.split(",");
                for(int i=0; i < arr.length; i++){
                    if((i+1)%2 == 0){
                        geo+= arr[i] + ";";
                    }else{
                        geo += arr[i] + ",";
                    }
                }
            }
            if(StringUtils.isNotEmpty(geo)){
                geo = geo.substring(0,geo.length() - 1);
            }
        } catch (IOException e) {
            logger.error("获取百度地图信息异常！", e);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("centerPoint",centerPoint);
        resultMap.put("geo",geo);
        return JsonResult.getResult(resultMap);
    }

    @Description("调用百度地图API获取行政区")
    @RequestMapping(value = "getDistrictMsg",method= RequestMethod.GET)
    @ResponseBody
    public JsonResult getDistrictMessage(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(value = "uid") String uid){
        String url = COMMON_BAIDU_URL + uid +"&da_par=direct&l=10";
        String geo ="";
        try {
            Map<String, Object> responseMap = HttpUtil.get(url);
            Map<String, Object> contentMap = MapUtils.getMap(responseMap,"content");
            String geoStr = MapUtils.getString(contentMap,"geo");
            if(StringUtils.isNotEmpty(geoStr)){
                geoStr = geoStr.substring(48, geoStr.length() - 1);
                String arr[]= geoStr.split(",");
                for(int i=0; i < arr.length; i++){
                    if((i+1)%2 == 0){
                        geo+= arr[i] + ";";
                    }else{
                        geo += arr[i] + ",";
                    }
                }
            }
            if(StringUtils.isNotEmpty(geo)){
                geo = geo.substring(0,geo.length() - 1);
            }
        } catch (IOException e) {
            logger.error("获取百度地图行政区信息异常！", e);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("geo",geo);
        return JsonResult.getResult(resultMap);
    }

}
